package com.cssiot.weixin.web.controller;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.cssiot.common.tools.ChkUtil;
import com.cssiot.weixin.basic.data.WeixinMenu;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

@Controller
@RequestMapping("/hello")
public class HelloRestController {
	@Autowired
	 private CacheManager cacheManager; 
	public static final String TOKEN = "jiaojing";
	

	
	
	@RequestMapping(value="/rest",method=RequestMethod.GET)
	public void restGet(HttpServletRequest req,HttpServletResponse response, Writer wr,@RequestParam(required=false) String str) throws IOException {
		response.setContentType( "text/javascript; charset=utf-8" );
		WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();  
        ServletContext servletContext = webApplicationContext.getServletContext(); 
        System.out.println("servletContext:"+servletContext);
		//设置接受跨域请求权限
		response.addHeader( "Access-Control-Allow-Origin", "*" ); 
		response.addHeader( "Access-Control-Allow-Methods", "GET" ); 
		response.addHeader( "Access-Control-Max-Age", "1000" );
		
		System.out.println("开台配置RequestMethod.GET");
		System.out.println("参数值为:"+str);
		
	/*	EhcacheUtil eu=EhcacheUtil.getInstance();
     	System.out.println("test:"+eu.get("user1", "test"));
     	eu.put("user1", "test", str);*/
		System.out.println("test5S----"+cacheManager.getCache("user1").get("test5S"));
 		System.out.println("test60S----"+cacheManager.getCache("user2").get("test60S"));
 		System.out.println("test7200S----"+cacheManager.getCache("dataCache").get("test7200S"));
     	if(!ChkUtil.isEmpty(str)){
//     		cacheManager.getCache("user1").put(new Element("test5S", str));
     		cacheManager.getCache("user1").put(new Element("test5S", str, 60, 60));
     		cacheManager.getCache("user2").put(new Element("test60S", str));
     		cacheManager.getCache("dataCache").put(new Element("test7200S", str));
     		System.out.println("test5S----"+cacheManager.getCache("user1").get("test5S").getObjectValue());
     		System.out.println("test60S----"+cacheManager.getCache("user2").get("test60S").getObjectValue());
     		System.out.println("test7200S----"+cacheManager.getCache("dataCache").get("test7200S"));
     		
     	}else{
     	}
    	
//    	System.out.println("test:"+eu.get("user1", "test"));
    	System.out.println("OK");
		
		
	}
	@RequestMapping(value="/rest",method=RequestMethod.POST)
	public void restPost(HttpServletRequest req,HttpServletResponse response, Writer wr) throws IOException {
		response.setContentType( "text/javascript; charset=utf-8" );
	    //设置接受跨域请求权限
		response.addHeader( "Access-Control-Allow-Origin", "*" ); 
		response.addHeader( "Access-Control-Allow-Methods", "POST" ); 
		response.addHeader( "Access-Control-Max-Age", "1000" );

		System.out.println("开台配置RequestMethod.POST");
		
	}
	
	@RequestMapping(value="/restOther",method=RequestMethod.POST)
	public void restOther(HttpServletRequest req,HttpServletResponse response, Writer wr ,@RequestBody WeixinMenu wx) throws IOException {
	    response.setContentType( "application/json;charset=utf-8" );
//		response.setContentType( "text/html; charset=utf-8" );
//	    System.out.println("json:"+json);
	    System.out.println("wx:"+wx.getName());
	    //设置接受跨域请求权限
		/*
		 response.setHeader("Access-Control-Allow-Origin", "*");
		 response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); 
		 response.setHeader("Access-Control-Max-Age", "3600"); 
		 response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization"); 
		 response.setHeader("Access-Control-Allow-Credentials","true");*/
		  

		System.out.println("开台配置RequestMethod.POST");
		
	}
	
	@RequestMapping(value="/restOther2",method=RequestMethod.POST)
	public void restOther2(HttpServletRequest req,HttpServletResponse response, Writer wr ,WeixinMenu wx,String name) throws IOException {
//	    response.setContentType( "application/json; charset=utf-8" );
		response.setContentType( "text/javascript; charset=utf-8" );
//	    System.out.println("json:"+json);
		 System.out.println("wx:"+wx.getName());
		 System.out.println("name:"+name);
	 
		
	}
	
	@RequestMapping(value="/rest",method=RequestMethod.PUT)
	public void restPut(HttpServletRequest req,HttpServletResponse response, Writer wr) throws IOException {
		response.setContentType( "text/javascript; charset=utf-8" );
		 //设置接受跨域请求权限
		response.addHeader( "Access-Control-Allow-Origin", "*" ); 
		response.addHeader( "Access-Control-Allow-Methods", "PUT" ); 
		response.addHeader( "Access-Control-Max-Age", "1000" );
 
		System.out.println("开台配置RequestMethod.PUT");
	}
	
	@RequestMapping(value="/rest",method=RequestMethod.DELETE)
	public void restDelete(HttpServletRequest req,HttpServletResponse response, Writer wr) throws IOException {
		response.setContentType( "text/javascript; charset=utf-8" );
		 //设置接受跨域请求权限
		response.addHeader( "Access-Control-Allow-Origin", "*" ); 
		response.addHeader( "Access-Control-Allow-Methods", "DELETE" ); 
		response.addHeader( "Access-Control-Max-Age", "1000" );
		
		System.out.println("开台配置RequestMethod.DELETE");
	}
	
	 
}